Simple job management page

Also added a seperate `job-indicator` for every job state
(pending/awaiting retry and failed)
The jobs page is automatically reloading when jobs are
enqueued, retried or failed.

Dominik Sander лет %!s(int64=9): %!d(string=назад)
Родитель
Сommit
830cf1bf3d

+ 0 - 3
Gemfile

@@ -45,9 +45,6 @@ gem 'delayed_job', '~> 4.0.0'
45 45
 gem 'delayed_job_active_record', '~> 4.0.0'
46 46
 gem 'daemons', '~> 1.1.9'
47 47
 
48
-# To enable DelayedJobWeb, see the 'Enable DelayedJobWeb' section of the README.
49
-# gem 'delayed_job_web'
50
-
51 48
 gem 'foreman', '~> 0.63.0'
52 49
 
53 50
 gem 'sass-rails',   '~> 4.0.0'

+ 26 - 13
app/assets/javascripts/worker-checker.js.coffee

@@ -1,22 +1,29 @@
1 1
 $ ->
2 2
   firstEventCount = null
3
+  previousJobs = null
3 4
 
4
-  if $("#job-indicator").length
5
+  if $(".job-indicator").length
5 6
     check = ->
6 7
       $.getJSON "/worker_status", (json) ->
7
-        firstEventCount = json.event_count unless firstEventCount?
8
-
9
-        if json.pending? && json.pending > 0
10
-          tooltipOptions = {
11
-            title: "#{json.pending} jobs pending, #{json.awaiting_retry} awaiting retry, and #{json.recent_failures} recent failures"
12
-            delay: 0
13
-            placement: "bottom"
14
-            trigger: "hover"
15
-          }
16
-          $("#job-indicator").tooltip('destroy').tooltip(tooltipOptions).fadeIn().find(".number").text(json.pending)
17
-        else
18
-          $("#job-indicator:visible").tooltip('destroy').fadeOut()
8
+        for method in ['pending', 'awaiting_retry', 'recent_failures']
9
+          count = json[method]
10
+          elem = $(".job-indicator[role=#{method}]")
11
+          if count > 0
12
+            tooltipOptions = {
13
+              title: "#{count} jobs #{method.split('_').join(' ')}"
14
+              delay: 0
15
+              placement: "bottom"
16
+              trigger: "hover"
17
+            }
18
+            if elem.is(":visible")
19
+              elem.tooltip('destroy').tooltip(tooltipOptions).find(".number").text(count)
20
+            else
21
+              elem.tooltip('destroy').tooltip(tooltipOptions).fadeIn().find(".number").text(count)
22
+          else
23
+            if elem.is(":visible")
24
+              elem.tooltip('destroy').fadeOut()
19 25
 
26
+        firstEventCount = json.event_count unless firstEventCount?
20 27
         if firstEventCount? && json.event_count > firstEventCount
21 28
           $("#event-indicator").tooltip('destroy').
22 29
                                 tooltip(title: "Click to reload", delay: 0, placement: "bottom", trigger: "hover").
@@ -26,6 +33,12 @@ $ ->
26 33
         else
27 34
           $("#event-indicator").tooltip('destroy').fadeOut()
28 35
 
36
+        currentJobs = [json.pending, json.awaiting_retry, json.recent_failures]
37
+        if document.location.pathname == '/jobs' && previousJobs? && previousJobs.join(',') != currentJobs.join(',')
38
+          $.get '/jobs', (data) =>
39
+            $("#main-content").html(data)
40
+        previousJobs = currentJobs
41
+
29 42
         window.workerCheckTimeout = setTimeout check, 2000
30 43
 
31 44
     check()

+ 4 - 3
app/assets/stylesheets/application.css.scss.erb

@@ -88,9 +88,10 @@ span.not-applicable:after {
88 88
 }
89 89
 
90 90
 // Navbar
91
-
92
-#job-indicator, #event-indicator {
93
-  display: none;
91
+.nav > li {
92
+  &.job-indicator, &#event-indicator {
93
+    display: none;
94
+  }
94 95
 }
95 96
 
96 97
 .navbar-search > .spinner {

+ 3 - 0
app/assets/stylesheets/jobs.css.scss

@@ -0,0 +1,3 @@
1
+.big-modal-dialog {
2
+  width: 90% !important;
3
+}

+ 4 - 0
app/controllers/application_controller.rb

@@ -14,6 +14,10 @@ class ApplicationController < ActionController::Base
14 14
     devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password) }
15 15
   end
16 16
 
17
+  def authenticate_admin!
18
+    redirect_to root_path unless current_user && current_user.admin
19
+  end
20
+
17 21
   def upgrade_warning
18 22
     return unless current_user
19 23
     twitter_oauth_check

+ 55 - 0
app/controllers/jobs_controller.rb

@@ -0,0 +1,55 @@
1
+class JobsController < ApplicationController
2
+  before_filter :authenticate_admin!
3
+
4
+  def index
5
+    @jobs = Delayed::Job.page(params[:page])
6
+
7
+    respond_to do |format|
8
+      format.html { render layout: !request.xhr? }
9
+      format.json { render json: @jobs }
10
+    end
11
+  end
12
+
13
+  def destroy
14
+    @job = Delayed::Job.find(params[:id])
15
+
16
+    respond_to do |format|
17
+      if !running? && @job.destroy
18
+        format.html { redirect_to jobs_path, notice: "Job deleted." }
19
+        format.json { render json: "", status: :ok }
20
+      else
21
+        format.html { redirect_to jobs_path, alert: 'Can not delete a running job.' }
22
+        format.json { render json: "", status: :unprocessable_entity }
23
+      end
24
+    end
25
+  end
26
+
27
+  def run
28
+    @job = Delayed::Job.find(params[:id])
29
+    @job.last_error = nil
30
+
31
+    respond_to do |format|
32
+      if !running? && @job.update_attributes!(run_at: Time.now, failed_at: nil)
33
+        format.html { redirect_to jobs_path, notice: "Job enqueued." }
34
+        format.json { render json: @job, status: :ok }
35
+      else
36
+        format.html { redirect_to jobs_path, alert: 'Can not enqueue a running job.' }
37
+        format.json { render json: "", status: :unprocessable_entity }
38
+      end
39
+    end
40
+  end
41
+
42
+  def destroy_failed
43
+    Delayed::Job.where.not(failed_at: nil).destroy_all
44
+
45
+    respond_to do |format|
46
+      format.html { redirect_to jobs_path, notice: "Failed jobs removed." }
47
+      format.json { render json: '', status: :ok }
48
+    end
49
+  end
50
+
51
+  private
52
+  def running?
53
+    @job.locked_at || @job.locked_by
54
+  end
55
+end

+ 4 - 0
app/helpers/application_helper.rb

@@ -38,4 +38,8 @@ module ApplicationHelper
38 38
       link_to 'No', agent_path(agent, tab: (agent.recent_error_logs? ? 'logs' : 'details')), class: 'label label-danger'
39 39
     end
40 40
   end
41
+
42
+  def user_is_admin?
43
+    current_user && current_user.admin == true
44
+  end
41 45
 end

+ 21 - 0
app/helpers/jobs_helper.rb

@@ -0,0 +1,21 @@
1
+module JobsHelper
2
+
3
+  def status(job)
4
+    case
5
+    when job.failed_at
6
+      content_tag :span, 'failed', class: 'label label-danger'
7
+    when job.locked_at && job.locked_by
8
+      content_tag :span, 'running', class: 'label label-info'
9
+    else
10
+      content_tag :span, 'queued', class: 'label label-warning'
11
+    end
12
+  end
13
+
14
+  def relative_distance_of_time_in_words(time)
15
+    if time < (now = Time.now)
16
+      time_ago_in_words(time) + ' ago'
17
+    else
18
+      'in ' + distance_of_time_in_words(time, now)
19
+    end
20
+  end
21
+end

+ 73 - 0
app/views/jobs/index.html.erb

@@ -0,0 +1,73 @@
1
+<div class='container'>
2
+  <div class='row'>
3
+    <div class='col-md-12'>
4
+      <div class="page-header">
5
+        <h2>
6
+          Background Jobs
7
+        </h2>
8
+      </div>
9
+
10
+      <div class='table-responsive'>
11
+        <table class='table table-striped events'>
12
+          <tr>
13
+            <th>Status</th>
14
+            <th>Created</th>
15
+            <th>Next Run</th>
16
+            <th>Attempts</th>
17
+            <th>Last Error</th>
18
+            <th></th>
19
+          </tr>
20
+
21
+        <% @jobs.each do |job| %>
22
+          <tr>
23
+            <td><%= status(job) %></td>
24
+            <td title='<%= job.created_at %>'><%= time_ago_in_words job.created_at %> ago</td>
25
+            <td title='<%= job.run_at %>'>
26
+              <% if !job.failed_at %>
27
+                <%= relative_distance_of_time_in_words job.run_at %>
28
+              <% end %>
29
+            </td>
30
+            <td><%= job.attempts %></td>
31
+            <td>
32
+              <a data-toggle="modal" data-target="#error<%= job.id %>"><%= truncate job.last_error, :length => 90, :omission => "", :separator => "\n" %></a>
33
+              <div class="modal fade" id="error<%= job.id %>" tabindex="-1" role="dialog" aria-labelledby="#<%= "error#{job.id}" %>" aria-hidden="true">
34
+                <div class="modal-dialog big-modal-dialog">
35
+                  <div class="modal-content">
36
+                    <div class="modal-header">
37
+                      <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
38
+                      <h4 class="modal-title" id="myModalLabel">Error Backtrace</h4>
39
+                    </div>
40
+                    <div class="modal-body">
41
+                      <%= raw html_escape(job.last_error).split("\n").join('<br/>') %>
42
+                    </div>
43
+                  </div>
44
+                </div>
45
+              </div>
46
+            </td>
47
+            <td>
48
+              <% if !job.locked_at && !job.locked_by %>
49
+                <div class="btn-group btn-group-xs" style="float: right">
50
+                  <% if job.run_at > Time.now %>
51
+                    <%= link_to 'Run now', run_job_path(job), class: "btn btn-default", method: :put %>
52
+                  <% end %>
53
+                  <%= link_to 'Delete', job_path(job), class: "btn btn-danger", method: :delete, data: { confirm: 'Really delete this job?' } %>
54
+                </div>
55
+              <% end %>
56
+            </td>
57
+          </tr>
58
+        <% end %>
59
+        </table>
60
+      </div>
61
+
62
+      <%= paginate @jobs, :theme => 'twitter-bootstrap-3' %>
63
+
64
+      <br />
65
+      <div class="btn-group">
66
+        <%= link_to destroy_failed_jobs_path, class: "btn btn-default", method: :delete do %>
67
+          <span class="glyphicon glyphicon-trash"></span> Remove failed jobs
68
+        <% end %>
69
+      </div>
70
+    </div>
71
+  </div>
72
+</div>
73
+

+ 18 - 10
app/views/layouts/_navigation.html.erb

@@ -35,15 +35,19 @@
35 35
         </div>
36 36
       </form>
37 37
       
38
-      <li id='job-indicator'>
39
-        <% if defined?(DelayedJobWeb) %>
40
-          <a href="/delayed_job">
41
-            <span class="badge"><span class="glyphicon glyphicon-refresh icon-white"></span> <span class='number'>0</span></span>
42
-          </a>
43
-        <% else %>
44
-          <a href="#" onclick='return false;'>
45
-            <span class="badge"><span class="glyphicon glyphicon-refresh icon-white"></span> <span class='number'>0</span></span>
46
-          </a>
38
+      <li class='job-indicator' role='pending'>
39
+        <%= link_to jobs_path do %>
40
+          <span class="badge"><span class="glyphicon glyphicon-refresh icon-white"></span> <span class='number'>0</span></span>
41
+        <% end %>
42
+      </li>
43
+      <li class='job-indicator' role='awaiting_retry'>
44
+        <%= link_to jobs_path do %>
45
+          <span class="badge"><span class="glyphicon glyphicon-question-sign icon-yellow"></span> <span class='number'>0</span></span>
46
+        <% end %>
47
+      </li>
48
+      <li class='job-indicator' role='recent_failures'>
49
+        <%= link_to jobs_path do %>
50
+          <span class="badge"><span class="glyphicon glyphicon-exclamation-sign icon-white"></span> <span class='number'>0</span></span>
47 51
         <% end %>
48 52
       </li>
49 53
       <li id='event-indicator'>
@@ -66,7 +70,11 @@
66 70
             <%= link_to 'Sign up', new_user_registration_path, :tabindex => "-1" %>
67 71
           <% end %>
68 72
         </li>
69
-
73
+        <% if user_signed_in? && current_user.admin %>
74
+          <li>
75
+            <%= link_to 'Job Management', jobs_path, :tabindex => '-1' %>
76
+          </li>
77
+        <% end %>
70 78
         <li>
71 79
           <%= link_to 'About', 'https://github.com/cantino/huginn', :tabindex => "-1" %>
72 80
         </li>

+ 3 - 1
app/views/layouts/application.html.erb

@@ -28,7 +28,9 @@
28 28
         <%= render "upgrade_warning" %>
29 29
       <% end %>
30 30
 
31
-      <%= yield %>
31
+      <div id="main-content">
32
+        <%= yield %>
33
+      </div>
32 34
       
33 35
     </div>
34 36
 

+ 1 - 1
config/initializers/delayed_job.rb

@@ -1,4 +1,4 @@
1
-Delayed::Worker.destroy_failed_jobs = true
1
+Delayed::Worker.destroy_failed_jobs = false
2 2
 Delayed::Worker.max_attempts = 5
3 3
 Delayed::Worker.max_run_time = 20.minutes
4 4
 Delayed::Worker.read_ahead = 5

+ 9 - 3
config/routes.rb

@@ -51,6 +51,15 @@ Huginn::Application.routes.draw do
51 51
     end
52 52
   end
53 53
 
54
+  resources :jobs, :only => [:index, :destroy] do
55
+    member do
56
+      put :run
57
+    end
58
+    collection do
59
+      delete :destroy_failed
60
+    end
61
+  end
62
+
54 63
   get "/worker_status" => "worker_status#show"
55 64
 
56 65
   post "/users/:user_id/update_location/:secret" => "user_location_updates#create"
@@ -58,9 +67,6 @@ Huginn::Application.routes.draw do
58 67
   match  "/users/:user_id/web_requests/:agent_id/:secret" => "web_requests#handle_request", :as => :web_requests, :via => [:get, :post, :put, :delete]
59 68
   post "/users/:user_id/webhooks/:agent_id/:secret" => "web_requests#handle_request" # legacy
60 69
 
61
-# To enable DelayedJobWeb, see the 'Enable DelayedJobWeb' section of the README.
62
-#  get "/delayed_job" => DelayedJobWeb, :anchor => false
63
-
64 70
   devise_for :users, :sign_out_via => [ :post, :delete ]
65 71
   get '/auth/:provider/callback', to: 'services#callback'
66 72
 

+ 67 - 0
spec/controllers/jobs_controller_spec.rb

@@ -0,0 +1,67 @@
1
+require 'spec_helper'
2
+
3
+describe JobsController do
4
+
5
+  describe "GET index" do
6
+    before do
7
+      Delayed::Job.create
8
+      Delayed::Job.create
9
+      Delayed::Job.count.should > 0
10
+    end
11
+
12
+    it "does not allow normal users"do
13
+      sign_in users(:bob)
14
+      get(:index).should redirect_to(root_path)
15
+    end
16
+    it "returns all jobs", focus: true do
17
+      sign_in users(:jane)
18
+      get :index
19
+      assigns(:jobs).length.should == 2
20
+    end
21
+  end
22
+
23
+  describe "DELETE destroy" do
24
+    before do
25
+      @not_running = Delayed::Job.create
26
+      @running = Delayed::Job.create(locked_at: Time.now, locked_by: 'test')
27
+      sign_in users(:jane)
28
+    end
29
+
30
+    it "destroy a job which is not running" do
31
+      expect { delete :destroy, id: @not_running.id }.to change(Delayed::Job, :count).by(-1)
32
+    end
33
+
34
+    it "does not destroy a running job" do
35
+      expect { delete :destroy, id: @running.id }.to change(Delayed::Job, :count).by(0)
36
+    end
37
+  end
38
+
39
+  describe "PUT run" do
40
+    before do
41
+      @not_running = Delayed::Job.create(run_at: Time.now - 1.hour)
42
+      @running = Delayed::Job.create(locked_at: Time.now, locked_by: 'test')
43
+      sign_in users(:jane)
44
+    end
45
+
46
+    it "queue a job which is not running" do
47
+      expect { put :run, id: @not_running.id }.to change { @not_running.reload.run_at }
48
+    end
49
+
50
+    it "not queue a running job" do
51
+      expect { put :run, id: @running.id }.not_to change { @not_running.reload.run_at }
52
+    end
53
+  end
54
+
55
+  describe "DELETE destroy_failed" do
56
+    before do
57
+      @failed = Delayed::Job.create(failed_at: Time.now - 1.minute)
58
+      @running = Delayed::Job.create(locked_at: Time.now, locked_by: 'test')
59
+      sign_in users(:jane)
60
+    end
61
+
62
+    it "just destroy failed jobs" do
63
+      expect { delete :destroy_failed, id: @failed.id }.to change(Delayed::Job, :count).by(-1)
64
+      expect { delete :destroy_failed, id: @running.id }.to change(Delayed::Job, :count).by(0)
65
+    end
66
+  end
67
+end

+ 2 - 1
spec/fixtures/users.yml

@@ -10,4 +10,5 @@ jane:
10 10
   email: "jane@example.com"
11 11
   username: jane
12 12
   invitation_code: <%= User::INVITATION_CODES.last %>
13
-  scenario_count: 1
13
+  scenario_count: 1
14
+  admin: true

+ 32 - 0
spec/helpers/jobs_helper_spec.rb

@@ -0,0 +1,32 @@
1
+require 'spec_helper'
2
+
3
+describe JobsHelper do
4
+  let(:job) { Delayed::Job.new }
5
+
6
+  describe '#status' do
7
+    it "works for failed jobs" do
8
+      job.failed_at = Time.now
9
+      status(job).should == '<span class="label label-danger">failed</span>'
10
+    end
11
+
12
+    it "works for running jobs" do
13
+      job.locked_at = Time.now
14
+      job.locked_by = 'test'
15
+      status(job).should == '<span class="label label-info">running</span>'
16
+    end
17
+
18
+    it "works for queued jobs" do
19
+      status(job).should == '<span class="label label-warning">queued</span>'
20
+    end
21
+  end
22
+
23
+  describe '#relative_distance_of_time_in_words' do
24
+    it "in the past" do
25
+      relative_distance_of_time_in_words(Time.now-5.minutes).should == '5m ago'
26
+    end
27
+
28
+    it "in the furute" do
29
+      relative_distance_of_time_in_words(Time.now+5.minutes).should == 'in 5m'
30
+    end
31
+  end
32
+end